using Engine;
using Engine.Graphics;
using Game;

namespace SCIENEW {
    /// <summary>
    /// 模型物品
    /// </summary>
    public class MeshItem : DefaultItem {
        public int DefaultTextureSlot;

        public BlockMesh m_standaloneBlockMesh = new();

        public Color Color;

        public Texture2D Texture;

        public MeshItem(string id) : base(id) {
            Id = id;
            Texture = IndustrialModLoader.BaseTexture;
        }

        /// <summary>
        /// 创建带有网格的物品
        /// </summary>
        /// <param name="name">展示的名称</param>
        /// <param name="id">唯一的标识id</param>
        /// <param name="modelName">模型名称，不用加"Models/"</param>
        /// <param name="meshName">网格名称</param>
        /// <param name="texture">材质</param>
        /// <param name="color">颜色</param>
        /// <param name="boneTransform">模型变换</param>
        /// <param name="tcTransform">材质变换</param>
        /// <param name="description">详细介绍</param>
        /// <param name="scale">缩放</param>
        /// <param name="transform2">绕Z轴旋转的角度</param>
        public MeshItem(string id, string modelName, string meshName, Texture2D texture, Color color, Matrix boneTransform, Matrix tcTransform, float scale = 1 ,float transform2 = 0f) : this(id)
        {
            Id = id;
            var model = ContentManager.Get<Model>( modelName);
            Matrix transform = BlockMesh.GetBoneAbsoluteTransform(model.FindMesh(meshName).ParentBone) * boneTransform * Matrix.CreateTranslation(0f, -0.4f, 0f) * Matrix.CreateScale(scale) * Matrix.CreateRotationZ(transform2);
            ModelMeshPart meshPart = model.FindMesh(meshName).MeshParts[0];
            m_standaloneBlockMesh.AppendModelMeshPart(meshPart, transform, false, false, false, false, Color.White);
            m_standaloneBlockMesh.AppendModelMeshPart(meshPart, transform, false, true, false, false, Color.White);
            Texture = texture;
            Color = color;
            m_standaloneBlockMesh.TransformTextureCoordinates(tcTransform);
        }
		public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData) {
            BlocksManager.DrawMeshBlock(
                primitivesRenderer,
                m_standaloneBlockMesh,
                Texture,
                Color * color,
                size,
                ref matrix,
                environmentData
            );
        }

        public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z) {
            generator.GenerateMeshVertices(
                BlocksManager.Blocks[Terrain.ExtractContents(value)],
                x,
                y,
                z,
                m_standaloneBlockMesh,
                Color.White,
                null,
                geometry.GetGeometry(Texture).SubsetOpaque
            );
        }

        public override float GetIconViewScale(int value, DrawBlockEnvironmentData environmentData) => 0.85f;

        public override bool IsFaceTransparent(SubsystemTerrain subsystemTerrain, int face, int value) => true;

        public MeshItem AppendMesh(string modelName, string meshName, Matrix boneTransform, Matrix tcTransform, Color color) {
            m_standaloneBlockMesh.AppendMesh(
                modelName,
                meshName,
                boneTransform,
                tcTransform,
                color
            );
            return this;
        }

        public override Vector3 GetFirstPersonOffset(int value) => new Vector3(0.5f, -0.5f, -0.6f);

        public override float GetFirstPersonScale(int value) => 0.4f;
    }
}